/*
 * @Author: sean_kei@163.com
 * @Date: 2021-06-08 15:07:29
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2021-11-12 10:33:28
 */
package com.wxhandle.cleandemo.application.services;

import java.io.Serializable;
import java.util.List;

import com.wxhandle.cleandemo.core.domain.base.QueryFilter;
import com.wxhandle.cleandemo.core.services.QueryableService;
import com.wxhandle.cleandemo.persistence.repositorys.DefaultRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;


@Service
public abstract class QueryServiceImpl<T,TKey extends Serializable> implements QueryableService<T,TKey> {

    @Autowired(required = true)
    protected DefaultRepository<T, TKey> defaultRepository;

    @Override
    public T getOne(TKey id) {
        return defaultRepository.getById(id);
    }
    @Override
    public T getFirst(QueryFilter<T> filter) {
        return defaultRepository.findOne(filter.getSpecification()).get();
    }
    @Override
    public Page<T> getPage(QueryFilter<T> filter, int page, int size) {
        if(null == filter)
            return defaultRepository.findAll(PageRequest.of(page, size));
        return defaultRepository.findAll(filter.getSpecification(), PageRequest.of(page, size, filter.getSort()));
    }
    @Override
    public List<T> getList(QueryFilter<T> filter) {
        if(null == filter)
            return defaultRepository.findAll();
        return defaultRepository.findAll(filter.getSpecification(), filter.getSort());
    }

    
}
